<template>
	<view>
		<cu-custom bgColor="bg-gradual-pink" :isBack="true">
			<block slot="backText">返回</block>
			<block slot="content">设置密码</block>
		</cu-custom>
		<form>

			<view class="cu-form-group margin-top">
				<view class="title">重置密码</view>
				<u-input v-model="password" :type="type" :border="border" :password-icon="passwordIcon" />
			</view>
			
			<u-button type="primary" shape="square" @click="baocun">保存</u-button>
		</form>
	</view>
</template>

<script>
	import {
		mapState,
		mapMutations
	} from 'vuex'
	export default {
		data() {
			return {
				password:"",
				type: 'password',
				passwordIcon: true,
				border: true,
			};
		},
		onLoad() {
		
			let uniIdToken = uni.getStorageSync('uni_id_token')
			if (uniIdToken) {
				uniCloud.callFunction({
					name: 'user-center',
					data: {
						action: 'checkToken',
					},
					success: (e) => {
						console.log('checkToken success个人设置', e);
						if (e.result.code > 0) {
							//token过期或token不合法，重新登录
							if (this.forcedLogin) {
								uni.reLaunch({
									url: '../login/login'
								});
							} else {
								uni.navigateTo({
									url: '../login/login'
								});
							}
						}
					},
					fail(e) {
						uni.showModal({
							content: JSON.stringify(e),
							showCancel: false
						})
					}
				})
			} else {
				this.guideToLogin()
			}
		},
		
		methods: {
			...mapMutations(['resetPwd']),
			guideToLogin() {
				let that =this;
				uni.showModal({
					title: '未登录',
					content: '您未登录，需要登录后才能继续',
					/**
					 * 如果需要强制登录，不显示取消按钮
					 */
					showCancel: !this.forcedLogin,
					success: (res) => {
						if (res.confirm) {
							that.goToLogin()
						}
					}
				});
			},
			goToLogin(){
				univerifyLogin().catch((err) => {
					// console.log(err,'???')
					if (err === false) return;
					/**
					 * 如果需要强制登录，使用reLaunch方式
					 */
					if (this.forcedLogin) {
						uni.reLaunch({
							url: '../login/login'
						});
					} else {
						uni.navigateTo({
							url: '../login/login'
						});
					}
				})
			},
			baocun(){
				
				// console.log(options)
				let that = this;
				uniCloud.callFunction({
					name:'user-center',
					data:{
						action:'resetPwd',
						params:{
							password:that.password
						}
					},
					success: (e) => {
						console.log(e)
						// uni.showToast({
						// 	title:'设置密码成功~'
						// })
						uni.removeStorageSync('user_id')
						uni.removeStorageSync('uni_id_token')
						uni.removeStorageSync('username')
						uni.removeStorageSync('uni_id_has_pwd')
						this.forcedLogin=true;
						this.resetPwd()
						//uni.$emit('checkUserIngo',true);//通知我的页面需要校验token
						uni.showModal({
							title: '密码设置成功',
							content: '设置密码需要重新登录~',
							/**
							 * 如果需要强制登录，不显示取消按钮
							 */
							showCancel: !this.forcedLogin,
							success: (res) => {
								if (res.confirm) {
									uni.redirectTo({
									    url: '/pages/login/login'
									});
								}
							}
						});
						// uni.$emit('checkUserIngo',true)
						// setTimeout(()=>{
						// 	uni.switchTab({
						// 		url:'./index'
						// 	})
						// },1000)
					},
					fail: (e) => {
						console.log(e,'更新个人信息失败')
					}
				})
			},
			
			
		}
	}
</script>

<style>
	.cu-form-group .title {
		min-width: calc(4em + 15px);
	}
</style>
